/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.WareCatBrandPo;
import com.itttq.gshop.domain.to.CatBrandTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.pbpm.service.WareCatBrandService;

@RestController
@RequestMapping("wareCatBrand")
public class WareCatBrandController extends GenericController {
	private static final long serialVersionUID = -8366968436640195L;
	public static final String CACHE_KEY = "wareCatBrand";
	@Autowired
	private WareCatBrandService wareCatBrandService;
	
	/**
	 * 根据条件获取WareCatBrandPo对象，并返回查询的数据总数
	 * 
	 * @param wareCatBrand
	 * @Author 迷蝶
	 * @return ResultPageTo<WareCatBrandPo>
	 */
	@RequestMapping("list")
    public ResultPageTo<WareCatBrandPo> getWareCatBrands(@RequestBody WareCatBrandPo wareCatBrand) {
        return wareCatBrandService.getWareCatBrands(wareCatBrand);
    }
	
	/**
	 * 根据ID获取WareCatBrandPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return WareCatBrandPo
	 */
	@RequestMapping("{id:^[0-9]*$}")
	@Cacheable(value=CACHE_KEY, key="'wareCatBrand_'+#id")
	public WareCatBrandPo getWareCatBrand(@PathVariable("id") int id) {
		return wareCatBrandService.getWareCatBrand(id);
	}
	
	/**
	 * 保存WareCatBrandPo对象，并清除此ID的缓存。<br>
	 * 如果ID存在，则更新对象；ID不存在则插入对象。
	 * 
	 * @param wareCatBrand
	 * @Author 迷蝶
	 * @return WareCatBrandPo
	 */
	@RequestMapping("save")
	@CacheEvict(value=CACHE_KEY, key="'wareCatBrand_'+#wareCatBrand.id")
	public Integer saveWareCatBrand(@RequestBody WareCatBrandPo wareCatBrand) {
		int rs = 0;
		if(null == wareCatBrand) {return rs;}
		if(null == wareCatBrand.getId()) {
			rs = wareCatBrandService.addWareCatBrand(wareCatBrand);
		} else {
			rs = wareCatBrandService.updateWareCatBrand(wareCatBrand);
		}
		return rs;
	}
	
	/**
	 * 根据ID删除WareCatBrandPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return WareCatBrandPo
	 */
	@RequestMapping("del/{id:^[0-9]*$}")
	@CacheEvict(value=CACHE_KEY, key="'wareCatBrand_'+#id")
	public Integer delWareCatBrand(@PathVariable("id") int id) {
		return wareCatBrandService.delWareCatBrand(id);
	}
	
	/**
	 * 获取所有打开的品牌分类，并按分类为key、品牌List为值放入Map中
	 * TODO GXG 按显示顺序排序
	 * 
	 * @return
	 * @Author 迷蝶
	 * @return Map<String,List<WareCatBrandPo>>
	 */
	@RequestMapping("allEnable")
	@CacheEvict(value=CACHE_KEY, key="'wareCatBrand_allEnable'")
	public Map<String, List<CatBrandTo>> getAllEnableCatBrands() {
		return wareCatBrandService.getAllEnableCatBrands();
	}
}